package com.zjut.an.util.system;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjut.an.admin.dao.AnAdmin;


public class ServletManager {

	public PrintWriter setEncoding(HttpServletRequest request, HttpServletResponse response)  {
		try {
			request.setCharacterEncoding("utf-8");
	        response.setContentType("text/html;charset=utf-8");
	        return response.getWriter();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;		
	}
	
	public void checkAdmin(HttpServletRequest request, HttpServletResponse response) {
		//安全控制，防止未登录过的用户的非法访问
		//如果用户未登录过，而请求servlet资源，则强制转向登录界面（AdminLogin.jsp）
		HttpSession session = request.getSession(true);
		AnAdmin AnAdmin = (AnAdmin)session.getAttribute("AnAdmin");
		try {
			if (AnAdmin == null) 
			{	
				response.sendRedirect(Config.getHomePath()+"AdminLogin.jsp");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public AnAdmin getAdmin(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		return (AnAdmin)session.getAttribute("AnAdmin");
	}
}
